import { isMobile } from './navigator'

/**
 * 判断是否在元素外触发事件
 * @param event  事件源
 * @param el 元素
 * @returns {*} true 在元素外 false 在元素内
 */
export function isOutEl(event, el) {
  let path = getEventPath(event)
  return !Array.from(path).includes(el)
}

/**
 * 获取事件冒泡路径
 * @description 兼容ie11,edge,chrome,firefox,safari
 * @param evt
 * @returns {Array}
 */
export function getEventPath(evt) {
  const path = (evt.composedPath && evt.composedPath()) || evt.path,
    target = evt.target;
  if (path != null) {
    return (path.indexOf(window) < 0) ? path.concat(window) : path;
  }
  if (target === window) {
    return [window];
  }
  const getParents = (node, memo) => {
    memo = memo || [];
    const parentNode = node.parentNode;

    if (!parentNode) {
      return memo;
    } else {
      return getParents(parentNode, memo.concat(parentNode));
    }
  }
  return [target].concat(getParents(target), window);
}

/**
 * 获取移动端和pc端触摸（pc端鼠标模拟）事件名对象
 */
export function getTouchEventNameMap() {
  const isM = isMobile()
  const nameMap = {
    touchStart: isM ? "touchstart" : 'mousedown',
    touchMove: isM ? "touchmove" : 'mousemove',
    touchEnd: isM ? "touchend" : 'mouseup',
  }
  return nameMap;
}

/**
 * 绑定事件的兼容代码
 * @param elem
 * @param type
 * @param handle
 */
export function addEvent(elem, type, handle) {
  if (elem.addEventListener) { //非ie和非ie9
    elem.addEventListener(type, handle, false);
  } else if (elem.attachEvent) { //ie6到ie8
    elem.attachEvent('on' + type, function () {
      handle.call(elem);
    })
  } else {
    elem['on' + type] = handle;
  }
}

/**
 * 解绑事件
 * @param elem
 * @param type
 * @param handle
 */
export function removeEvent(elem, type, handle) {
  if (elem.removeEventListener) { //非ie和非ie9
    elem.removeEventListener(type, handle, false);
  } else if (elem.detachEvent) { //ie6到ie8
    elem.detachEvent('on' + type, handle);
  } else {
    elem['on' + type] = null;
  }
}

/**
 * 取消冒泡的兼容代码
 * @param e
 */
export function stopBubble(e) {
  if (e && e.stopPropagation) {
    e.stopPropagation();
  } else {
    window.event.cancelBubble = true;
  }
}
function ElasticMovement(obj, target) {
  clearInterval(obj.timer);
  var iSpeed = 40,
    a, u = 0.8;
  obj.timer = setInterval(function () {
    a = (target - obj.offsetLeft) / 8;
    iSpeed = iSpeed + a;
    iSpeed = iSpeed * u;
    if (Math.abs(iSpeed) <= 1 && Math.abs(a) <= 1) {
      console.log('over')
      clearInterval(obj.timer);
      obj.style.left = target + 'px';
    } else {
      obj.style.left = obj.offsetLeft + iSpeed + 'px';
    }
  }, 30);
}

function animate(obj, json, callback) {
  clearInterval(obj.timer);
  var speed,
    current;
  obj.timer = setInterval(function () {
    var lock = true;
    for (var prop in json) {
      if (prop == 'opacity') {
        current = parseFloat(window.getComputedStyle(obj, null)[prop]) * 100;
      } else {
        current = parseInt(window.getComputedStyle(obj, null)[prop]);
      }
      speed = (json[prop] - current) / 7;
      speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);

      if (prop == 'opacity') {
        obj.style[prop] = (current + speed) / 100;
      } else {
        obj.style[prop] = current + speed + 'px';
      }
      if (current != json[prop]) {
        lock = false;
      }
    }
    if (lock) {
      clearInterval(obj.timer);
      typeof callback == 'function' ? callback() : '';
    }
  }, 30)
}
